import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.*;

@Data
@NoArgsConstructor
public class TbSeed extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** id */
    private Long id;

    /** 种子 */
    private String seed;

    /** 种子 */
    private String hashCode;

    /** 协议类型 */
    private String agreement;

    /** 下载状态 0未下载 1已下载 */
    private Integer downloadStatus;

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                .append("id", getId())
                .append("seed", getSeed())
                .append("agreement", getAgreement())
                .append("downloadStatus", getDownloadStatus())
                .append("createTime", getCreateTime())
                .append("updateBy", getUpdateBy())
                .append("updateTime", getUpdateTime())
                .append("remark", getRemark())
                .toString();
    }
    public TbSeed(String seed, String agreement, Integer downloadStatus,String createBy, Date createTime){
        this.seed=seed;
        this.agreement=agreement;
        this.downloadStatus=downloadStatus;
        this.setCreateBy(createBy);
        this.setCreateTime(createTime);
    }

    public List<TbSeed> checkTbSeed(){
        Date createTime=new Date();
        String createBy= "admin";
        List<TbSeed> list = new ArrayList<>();
        for (String seed : this.getSeed().split("\n")) {
            TbSeed tbSeed = toTbSeed(seed);
            if (Objects.nonNull(tbSeed)){
                list.add(tbSeed);
            }
        }
        return list;
    }

    public TbSeed toTbSeed(String seed) {
        if (StringUtils.isBlank(seed)) {
            return null;
        }
        if (seed.indexOf("magnet:?xt=urn:btih:")>-1){
            TbSeed tbSeed = new TbSeed();
            String hashCode = seed.substring(20);
            hashCode = hashCode.substring(0,40);;
            tbSeed.setSeed(seed);
            tbSeed.setHashCode(hashCode);
            tbSeed.setAgreement("磁力链接");
            return tbSeed;
        }
        if (seed.indexOf("thunder://")>-1){
            TbSeed tbSeed = new TbSeed();
            String hashCode = seed.substring(10);
            hashCode = hashCode.substring(0,hashCode.indexOf("="));
            tbSeed.setSeed(seed);
            tbSeed.setHashCode(hashCode);
            tbSeed.setAgreement("迅雷链接");
            return tbSeed;
        }
        return null;
    }

    public static void main(String[] args) {
        TbSeed tbSeed = new TbSeed();
        tbSeed.setSeed("magnet:?xt=urn:btih:8181A12265E961A117689C9616804170D6BA63DD\n" +
                "thunder://QUFtYWduZXQ6P3h0PXVybjpidGloOkQ0OEIwMjMyOEVGNDkyQTZENEYwMTQxODVCNDQ4RjlGNDZEODI0QjFaWg==\n" +
                "magnet:?xt=urn:btih:0ff611f52c31e7862c7959ac7db0a47436e05cfa&dn");
        for (TbSeed seed : tbSeed.checkTbSeed()) {
            System.out.println(seed.getHashCode());
        }

    }

}
